package engine.newUi
{
	import engine.GameData;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.display.newAttack.AttackLogic;
	import engine.event.SceneEvent;
	import engine.flag.PlayerActionFlag;
	import engine.logic.LogSendLogic;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.ui.AlertUI;
	import engine.ui.MainLogic;
	import engine.ui.MainUIManager;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;

	/**
	 * 打坐逻辑
	 * @author liuel
	 * 
	 */	
	public class SitCrossleggedLogic
	{	
		private  	static	var		_self				:SitCrossleggedLogic;
		private 	var				_ui					:SitCrossleggedUI;
		//监听鼠标事件的Timer
		private	var				_sleepTimer			:Timer;
		//用户停止操作的计时
		private	var				sleeptime			:int	;
		private	var				_model				:ModelLocator;
		private 	var				_bingManage			:BindManage;
		private	var				_time				:Timer;
		//打坐剩余时间
		private	var				_date				:Date;
		//打坐加速时间
		private	var				_speeddata			:Date;
		//当前作用的按钮
		private	var				_indexBtn			:int;
		//停止操作的时间 开始进入打坐
		private	const			TIME_SIT			:int	=	1*60*1000;
		//心跳
		private    const			HEART            	:int	=	1*1000;
		//0没有第一个按钮 1有第一个按钮
		private	var				btn1				:int;
		//0没有第二个按钮 1有第二个按钮
		private	var				btn2				:int;
		private	var				_indexOneCount		:int	=	-1;
		private 	var				_indexTwoCount		:int	=	-1;
		
		private	var				_isSit				:Boolean;
		
		private 	var				_currIndex			:int	=1;
		/**
		 *打坐最低等级 
		 */		
		private	const			SIT_LEVEL:int =12;
		
		private 	var				_show				:Boolean;
		public static function getInstance():SitCrossleggedLogic
		{
			if(_self == null)
			{
				_self = new SitCrossleggedLogic();
			}
			return _self;
		}
		public function Init():void
		{
			_date		=	new Date();
			_date.time 	=	0;
			_speeddata	=	new Date();
			_time   	=	new Timer(1000);
			_sleepTimer	=	new Timer(HEART);
			
			_sleepTimer	.addEventListener(TimerEvent.TIMER,onListenerHandler);
			
			_ui			=  SitCrossleggedUI.getInstance();
			_bingManage	=BindManage.getInstance();
			_model		=  ModelLocator.getInstance();
			_model		.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_SIT_INFO),onOpen);
			_model		.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_COUPLEAFK_REQUEST),onCloseMsgBox);
			_model		.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_SIT_SPEED_INFO),getSpeedInfo);
			_model		.addEventListener( SceneEvent.SceneLoaded	,onSceneLoaded );
			
			_ui.gaojiMc.visible = false;
			_ui.gaojiMc.mouseChildren = _ui.gaojiMc.mouseEnabled = false;
			_ui.gaojiMc.stop();
			
			_ui.addEventListener( MouseEvent.MOUSE_OUT, onOutBtn );
//			StageInfo	.stage.addEventListener(MouseEvent.MOUSE_DOWN,onClickStage);

		}
		private function onCloseMsgBox(e:CustomMsgEvent):void
		{
			if(MainLogic.getInstance().currMsgStrID == "99273")
			{
				AlertUI.hideAlert();
			}
		}
		private function bind():void
		{
			if(_indexOneCount == -1)
			{
				_indexOneCount = _bingManage.BindProp(_model.scene.Role,"BuyOneHourStatus",this,"state1");
			}
			if(_indexTwoCount == -1)
			{
				_indexTwoCount = _bingManage.BindProp(_model.scene.Role,"BuyMaxHourStatus",this,"state2");
			}
		}
		private function unbind():void
		{
			if(_indexOneCount != -1)
			{
				_indexOneCount=_bingManage.UnbindProp(_model.scene.Role,"BuyOneHourStatus",_indexOneCount);
			}
			if(_indexTwoCount != -1)
			{
				_indexTwoCount=_bingManage.UnbindProp(_model.scene.Role,"BuyMaxHourStatus",_indexTwoCount);
			}
		}
		public function SitCrossleggedLogic()
		{
		}
		public function BeforeShow():void
		{
			bind();
			addEvent();
		}
		public function AfterHide():void
		{
			unbind();
			_time.stop();
			removeEvent();
		}
		private function addEvent():void
		{

			_time       .addEventListener(TimerEvent.TIMER,onPlayTimer);
			_ui.clostBtn.addEventListener(MouseEvent.CLICK,onClose);
			_ui.btn1    .addEventListener(MouseEvent.CLICK,onClickBtn1);
			_ui.btn2    .addEventListener(MouseEvent.CLICK,onClickBtn2);
			_ui.btn1    .addEventListener(MouseEvent.MOUSE_OVER,onOverBtn);
			_ui.btn1    .addEventListener(MouseEvent.MOUSE_OUT,onOutBtn);
			_ui.btn2    .addEventListener(MouseEvent.MOUSE_OVER,onOverBtn);
			_ui.btn2    .addEventListener(MouseEvent.MOUSE_OUT,onOutBtn);
		}
		private function removeEvent():void
		{
			_time       .removeEventListener(TimerEvent.TIMER,onPlayTimer);
			_ui.clostBtn.removeEventListener(MouseEvent.CLICK,onClose);
			_ui.btn1    .removeEventListener(MouseEvent.CLICK,onClickBtn1);
			_ui.btn2    .removeEventListener(MouseEvent.CLICK,onClickBtn2);
			_ui.btn1    .removeEventListener(MouseEvent.MOUSE_OVER,onOverBtn);
			_ui.btn1    .removeEventListener(MouseEvent.MOUSE_OUT,onOutBtn);
			_ui.btn2    .removeEventListener(MouseEvent.MOUSE_OVER,onOverBtn);
			_ui.btn2    .removeEventListener(MouseEvent.MOUSE_OUT,onOutBtn);
		}
		/**
		 * 转换时间 
		 * @param _date
		 * @return 
		 * 
		 */		
		private function getTimeStr(_date:Date,type:int):String
		{
			var hours  :int  = _date.hoursUTC;
			var minutes:int  = _date.minutesUTC;
			var seconds:int  = _date.secondsUTC;
			var str:String;
			if(type	== 1)
			{
			    str = initTime(hours)+":"+initTime(minutes)+":"+initTime(seconds);
			}
			else
			{
				//只获取小时用于加速按钮Tips和提示用
				str=hours.toString();
			}
            
			return str;	
		}
		/**
		 * Tips信息 
		 * @param strID 按钮
		 * @param arr 数据
		 * 
		 */		
		private function setTipsStr(strID:String,arr:Array):String
		{
			var tips:String = StaticIni.getSwfLangStrVar(strID,arr);
			return tips;
		}
		/**
		 * 显示字符 
		 * @param str
		 * 
		 */		
		private function getLabelStr(str:String):void
		{
			_ui.timeLabel.text = StaticIni.getSwfLangStr("UIString_1000029")+str;
		}
		/**
		 * 格式时间 
		 * @param val
		 * @return 
		 * 
		 */		
		private function initTime(val:int):String
		{
			var  va:String = val.toString();
			val>9?va:va="0"+va;
			return va;
		}
		/**
		 * 开始打坐 
		 * 
		 */		
		public function startSit():void
		{
			var walkState:Boolean=SceneManager.getInstance().norSceneView.haveWalkIocn;
			if(walkState)
			{
				sleeptime =0; 
				return ;
			}
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_STATR_ZAZEN);
			sleeptime =0; 
			
			_isSit = true;
		}
		/**
		 * 结束打坐
		 * 
		 */		
		public function stopSit():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_STOP_ZZEN);
			sleeptime = 0;
			_ui.hide();
			
			_sleepTimer.start();
			
			if( null != SceneManager.getInstance().norSceneView )
			{
				GameData.Role.status = PlayerActionFlag.NormalWait;
			}
			
			_isSit = false;
		}
		/**
		 * 确定加速 
		 * 
		 */			
		private function backFun():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_SPEED_ASCERTAIN,_currIndex);
		}
		public function set state1(value:int):void 
		{
			value==1?_ui.btn1.visible=false:_ui.btn1.visible=true;
		}
		

		public function set state2(value:int):void
		{
			value==1?_ui.btn2.visible=false:_ui.btn2.visible=true;
		}
		///////////////////////////////////////////////////////////////监听函数//////////////////////////////////////////////////////////////////////
		/**
		 * 玩家进入场景开始监听 
		 * @param e
		 * 
		 */		
		private function onSceneLoaded(e:SceneEvent):void
		{
			_model.removeEventListener(SceneEvent.SceneLoaded	, onSceneLoaded );
			_sleepTimer.start();
		
		}
		
		/**
		 * 接受作用按钮的消息 
		 * @param e
		 * 
		 */		
		private function getSpeedInfo(e:CustomMsgEvent):void
		{
			var type:int	=	e.pList[0]; //0 显示Tips 1显示提示
			_indexBtn		=	e.pList[1];
	        var time:int	=	e.pList[2]; //单位时
			var gold:int	=	e.pList[3]; //元宝
			var exp :int	=	e.pList[4]; //经验
//			_speeddata.time	=	time*1000;
			var timestr:String	=time.toString();	//getTimeStr(_speeddata,0);
			var infoArr:Array = [gold,timestr,exp];
			switch(type)
			{
				case 0:
					var tips:String=setTipsStr("UIString_1000021",infoArr);
					TipsMainLogic.instance.showString(tips);
					break;
				case 1:
					if(SitAlerUI.getInstance().created == false)
					{
						var targe:NxButton;
						if(_indexBtn == 1)
						{
							SitAlerLogic.getIntatic().setUIinfo("UIString_1000021",infoArr,backFun,_ui.btn1);
						}
						else
						{
							SitAlerLogic.getIntatic().setUIinfo("UIString_1000021",infoArr,backFun,_ui.btn2);
						}
					}
					break;
			}
		}
		private function onListenerHandler(e:TimerEvent):void
		{
			sleeptime+=HEART;
			if(sleeptime>=TIME_SIT && !_ui.visible && !MainUIManager.getInstance().autoFoundWay)
			{
				var lv:int	= _model.scene.Role.getPlayerLevel();
				if(lv>=SIT_LEVEL)
				{
					startSit();
				}
				
			}
		}
		
		public function onClickStage():void
		{
			sleeptime 	=	0;
//            //点击的是场景
			if( _ui.visible )
			{
				stopSit();
			}
			
		}

		private function onOpen(e:CustomMsgEvent=null):void
		{
			var isHaveFriend:int;  //是否双人挂机
			var name:String;
			var addexp:int=0;
			isHaveFriend = _model.scene.Role.GetProperties("CoupleAFKState");
			var startime:int	=	e.pList[0];
			
			if(startime<=0)
			{
				if(_ui.visible)
				{
					stopSit();
				}else
				{
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_STOP_ZZEN);
				}
				return;
			}
			
			_isSit = true;
				
			if (_ui.created == false)
			{
				 _ui.show();
			}
			_ui.visible 		= !AttackLogic.getInstance().fighting;
			var titalStr:String ="";
			var addexpStr:String="";
			if(isHaveFriend!=1)
			{
				titalStr=StaticIni.getSwfLangStr("UIString_1000027")
				addexpStr ="";
				_ui.gaojiMc.visible = false;
				_ui.gaojiMc.stop();
			}
			else
			{
				addexp =e.pList[2];
				name =_model.scene.Role.GetProperties("CoupleAFKName");
				titalStr=StaticIni.getSwfLangStrVar("UIString_1000517",[name]);
				addexpStr=StaticIni.getSwfLangStrVar("UIString_1000518",[addexp])
				_ui.gaojiMc.visible = true;
				_ui.gaojiMc.play();
			}
			_ui.label.htmlText  = titalStr;
			_ui.expLabel.htmlText 	= StaticIni.getSwfLangStr("UIString_1000028")+e.pList[1]+addexpStr;
			_date.time		  	= startime*1000;
			_time.start();
			_sleepTimer.reset();
			_sleepTimer.stop();
			
			if( !_show )
			{
				_ui.visible = false;
			}
			
			//人物模型改变
//			SceneManager.getInstance().norSceneView.scenePlayerLogic.role.status = PlayerActionFlag.Sit;
			
			GameData.Role.sit();
		
		}
		private function onPlayTimer(e:TimerEvent):void
		{
			_date.time-=1000;
			var str:String=getTimeStr(_date,1);
			getLabelStr(str);
			if(_date.time <= 0)
			{
				_time.reset();
				_ui.hide();
				sleeptime	=	0;
			}
		}
		/**
		 * 取消打坐 
		 * @param e
		 * 
		 */		
		private function onClose(e:MouseEvent):void
		{
			stopSit();
		}
		
		/**
		 * 点击加速一
		 * @param e
		 * 
		 */		
		private function onClickBtn1(e:MouseEvent):void
		{
			
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_SPEED_INFO,1,1);
			_currIndex = 1;
			LogSendLogic.getInstance().btnClickNumSend(22838)
		}
		/**
		 * 点击加速二
		 * @param e
		 * 
		 */		
		private function onClickBtn2(e:MouseEvent):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_SPEED_INFO,1,2);
			_currIndex = 2;
			LogSendLogic.getInstance().btnClickNumSend(22838)
		}
		private function onOverBtn(e:MouseEvent):void
		{
			var mc:Object = e.currentTarget
			if(mc == null)
				return;
			if(mc == _ui.btn1)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_SPEED_INFO,0,1);
			}
			else
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SEND_SPEED_INFO,0,2);
			}
		}
		private function onOutBtn(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}

		public function get isSit():Boolean
		{
			return _isSit;
		}

		public function set isSit(value:Boolean):void
		{
			_isSit = value;
		}

		public function get show():Boolean
		{
			return _show;
		}

		public function set show(value:Boolean):void
		{
			_show = value;
			
			if( !_show )
			{
				_ui.visible = false;
			}
			if( _show && _isSit )
			{
				_ui.visible = true;
			}
		}
	}
}